function fetchCardData(source, sectors) {
  let target = [];
  if (source.length == 0) {
    return [];
  }

  for (let i = 0; i < source.length; i++) {
    let converge = false;
    for (let j = 0; j < target.length; j++) {
      if (
        target[j].sector == source[i].handin ||
        target[j].sector == source[i].handout
      ) {
        converge = true;
        let merge = false;
        for (let k = 0; k < target[j].content.length; k++) {
          if (
            target[j].content[k].levels.join("") == source[i].levels.join("")
          ) {
            merge = true;
            target[j].content[k].details.push(source[i]);
            if (
              source[i].remark.length > 0 &&
              target[j].content[k].remarks.indexOf(source[i].remark) < 0
            ) {
              target[j].content[k].remarks.push(source[i].remark);
            }
          }
        }
        if (!merge) {
          let type = "in";
          if (sectors.indexOf(source[i].handin) < 0) type = "out";
          let remarks = [];
          if (source[i].remark.length > 0) remarks.push(source[i].remark);
          target[j].content.push({
            type,
            levels: source[i].levels,
            restrictedLevels: source[i].restrictedLevels,
            details: [source[i]],
            remarks,
          });
        }
      }
    }
    if (!converge) {
      let sector = source[i].handout;
      let type = "in";
      if (sectors.indexOf(source[i].handin) < 0) {
        sector = source[i].handin;
        type = "out";
      }
      let remarks = [];
      if (source[i].remark.length > 0) remarks.push(source[i].remark);
      target.push({
        sector: sector,
        content: [
          {
            type,
            levels: source[i].levels,
            restrictedLevels: source[i].restrictedLevels,
            details: [source[i]],
            remarks,
          },
        ],
      });
    }
  }
  return target;

  // this.card = target;

  // this.transferStore.setData({
  //   module: "transfer",
  //   key: "card",
  //   data: target,
  // });
}

function fetchTableData(source, sectors) {
  let tableData = { out: [], in: [] };
  for (let i = 0; i < source.length; i++) {
    let converge = false;
    let target = tableData.out;
    if (sectors.indexOf(source[i].handin) > -1) target = tableData.in;
    for (let j = 0; j < target.length; j++) {
      if (
        target[j].levels.join(",") == source[i].levels.join(",") &&
        (target[j].handin.indexOf(source[i].handin) > -1 ||
          target[j].handout.indexOf(source[i].handout) > -1)
      ) {
        converge = true;
        if (target[j].handout.indexOf(source[i].handout) < 0) {
          target[j].handout.push(source[i].handout);
        }
        if (target[j].handin.indexOf(source[i].handin) < 0) {
          target[j].handin.push(source[i].handin);
        }
        if (target[j].route.indexOf(source[i].route) < 0) {
          target[j].route.push({
            route: source[i].route,
            airports: source[i].airports,
            remark: source[i].remark,
          });
        }
      }
    }
    if (!converge) {
      //JSON.parse(JSON.stringify())
      let line = JSON.parse(JSON.stringify(source[i]));
      //format
      line.handin = [line.handin];
      line.handout = [line.handout];
      line.route = [
        { route: line.route, airports: line.airports, remark: line.remark },
      ];
      target.push(line);
    }
  }
  return tableData;
}

export { fetchCardData, fetchTableData };
